package com.student.dong.service.impl;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.student.dong.domain.AlgsStudent;
import com.student.dong.entity.AlgsCourseSelectionRecord;
import com.student.dong.entity.AlgsCurriculaVarible;
import com.student.dong.entity.AlgsEvaluate;
import com.student.dong.mapper.AlgsCurriculaVaribleMapper;
import com.student.dong.service.*;
import com.student.dong.vo.CurriculaVaribleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Program: animalSystem->IAlgsCurriculaVaribleServiceImpl
 * @Description:
 * @Author: donghang
 * @Date: 2023-03-20 14:32
 * @Version： 1.0
 **/
@Service
public class IAlgsCurriculaVaribleServiceImpl implements IAlgsCurriculaVaribleService {

    @Autowired
    private IAlgsStudentService algsStudentService;

    @Autowired
    private IEvaluateService evaluateService;

    int i = 0;

    @Autowired
    private AlgsCurriculaVaribleMapper algsCurriculaVaribleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return algsCurriculaVaribleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(AlgsCurriculaVarible record) {
        return algsCurriculaVaribleMapper.insert(record);
    }

    @Override
    public int insertSelective(AlgsCurriculaVarible record) {
        return algsCurriculaVaribleMapper.insertSelective(record);
    }

    @Override
    public AlgsCurriculaVarible selectByPrimaryKey(Long id) {
        return algsCurriculaVaribleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CurriculaVaribleVo> selAll() {
        return algsCurriculaVaribleMapper.selAll();
    }

    @Override
    @Transactional
    public int addClass(Integer curriculaVaribleId, String curriculaVaribleBy2) {
        AlgsCurriculaVarible algsCurriculaVarible = algsCurriculaVaribleMapper.selectByPrimaryKey(curriculaVaribleId.longValue());
        //当前课程报名人数
        Integer curriculaVaribleStudentNum = algsCurriculaVarible.getCurriculaVaribleStudentNum();
        //当前课程最大容纳人数
        Integer curriculaVaribleMax = algsCurriculaVarible.getCurriculaVaribleMax();
        //获取老师ID
        Integer teacherId = algsCurriculaVarible.getTeacherId();
        //判断当前课程是否还有位置
        if(curriculaVaribleStudentNum > curriculaVaribleMax){
            //满员
            return 1;
        }else {
            Long userId = SecurityUtils.getUserId();
            System.out.println(userId);
            //报名学生对象
            AlgsStudent algsStudent = algsStudentService.selectByUserId(userId);
            Long id = algsStudent.getStudentId();
            Integer studentId = id.intValue();
            AlgsEvaluate evaluate = evaluateService.selectByStudentId(studentId,curriculaVaribleId.longValue());
            if (evaluate == null) {
                AlgsEvaluate algsEvaluate = new AlgsEvaluate();
                algsEvaluate.setStudentId(studentId);
                algsEvaluate.setTeacherId(teacherId);
                algsEvaluate.setCurriculaVaribleId(curriculaVaribleId);
                evaluateService.insertSelective(algsEvaluate);
                i++;
                algsCurriculaVarible.setCurriculaVaribleStudentNum(i);
                algsCurriculaVaribleMapper.updateByPrimaryKeySelective(algsCurriculaVarible);
                return 2;
            }
            return 3;
        }
    }

    @Override
    public int updateByPrimaryKeySelective(AlgsCurriculaVarible record) {
        return algsCurriculaVaribleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AlgsCurriculaVarible record) {
        return algsCurriculaVaribleMapper.updateByPrimaryKey(record);
    }
}
